package com.lyfx.ly.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lyfx.common.valid.EditStatusGroup;
import com.lyfx.ly.entity.ClassCourseAttrValuesEntity;
import com.lyfx.ly.entity.ClassCourseEntity;
import com.lyfx.ly.service.ClassCourseAttrValuesService;
import com.lyfx.ly.vo.AttrRespVo;
import com.lyfx.ly.vo.AttrVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lyfx.ly.entity.AttrEntity;
import com.lyfx.ly.service.AttrService;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.R;



/**
 * 
 *
 * @author lsy
 * @email lsy@gmail.com
 * @date 2020-07-11 17:57:41
 */
@RestController
@RequestMapping("ly/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ClassCourseAttrValuesService classCourseAttrValuesService;

    /**
     * /ly/attr/base/listforcourse/3
     * 查出课程的属性
     */
    @GetMapping("/base/listforcourse/{courseId}")
    public R baseListForCourse(@PathVariable("courseId") Long courseId){

        List<ClassCourseAttrValuesEntity> data = classCourseAttrValuesService.baseAttrForCourse(courseId);
        return R.ok().put("data", data);
    }
    /**
     * /api/ly/attr/update/2
     * 修改某个课程的属性
     */
    @PostMapping("/update/{courseId}")
    public R attrUpdate(@PathVariable("courseId") Long courseId,
                        @RequestBody List<ClassCourseAttrValuesEntity> entities){
        classCourseAttrValuesService.updateCourseAttr(courseId,entities);
        return R.ok();
    }

    /**
     * 查询对应的三级分类的属性
     */
    @GetMapping("/list/{catelogId}")
    // @RequiresPermissions("ly:attr:list")
    public R listcatelogId(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId ){
        PageUtils page = attrService.queryAttrPage(params,catelogId);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("ly:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
   // @RequiresPermissions("ly:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("ly:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("ly:attr:update")
    public R update(@RequestBody AttrVo attr){
        // TODO 除了修改自己的还需要修改关联表
		attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 修改状态信息
     */
    @RequestMapping("/update/status")
    @RequiresPermissions("ly:attr:update")
    public R updateStatus(@RequestBody AttrEntity attr){
        attrService.updateById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("ly:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
